<template>
	<view class="main" v-if="datas.content">
		<image src="../../static/image/growla.png" mode=""></image>
		<view class="version">
			{{versionCode}}
		</view>
		<view class="update" v-if="versionCode == datas.version">
			<view class="texts">
				当前已是最新版本
			</view>
		</view>
		<view class="update" v-else>
			<view class="text">
				发现新版本
			</view>
			<view class="btn" @click="toUpdate">
				立即更新
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				versionCode: "1.01",
				datas: {}
			}
		},
		async onLoad() {
			await this.getVersion()
			// this.versionCode = plus.runtime.version()
		},
		methods: {
			getVersion() {
				this.$httpRequest({
					url: "app/api/aboutus.php",
					method: "POST"
				}).then(res => {
					this.datas = res
				})
			},
			toUpdate() {
				//判断是否为安卓环境
				if (systemInfo.platform.toLowerCase() === 'android') {
					var _this = this;
					uni.showLoading({
						title: '更新中……'
					})
					uni.downloadFile({ //执行下载
						url: _this.datas.filecotent, //下载地址
						success: downloadResult => { //下载成功
							uni.hideLoading();
							if (downloadResult.statusCode == 200) {
								uni.showModal({
									title: '',
									content: '更新成功，确定现在重启吗？',
									confirmText: '重启',
									confirmColor: '#4DBEC1',
									success: function(res) {
										if (res.confirm == true) {
											plus.runtime.install( //安装
												downloadResult.tempFilePath, {
													force: true
												},
												function(res) {
													uni.showToast({
														title: "更新成功，重启中",
														icon: "none"
													})
													plus.runtime.restart();
												}
											);
										}
									}
								});
							}
						}
					});
				} else {
					//iOS跳转到App Store下载
					
					let appleId = 1611495320
					plus.runtime.launchApplication({
						action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
					}, function(e) {
						console.log('Open system default browser failed: ' + e.message);
					});

				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		width: 90%;
		margin: 0 auto;
		text-align: center;

		image {
			display: block;
			width: 236rpx;
			height: 236rpx;
			margin: 38rpx auto;
		}

		.version {

			font-size: 28rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #6E7580;
		}

		.update {
			margin-top: 20rpx;

			.text {
				font-size: 24rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #4DBEC1;
			}

			.texts {
				font-size: 24rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #6E7580;
			}

			.btn {
				width: 100%;
				height: 88rpx;
				background: #4DBEC1;
				border-radius: 110rpx;
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 19px;
				line-height: 88rpx;
				margin-top: 66rpx;
			}
		}
	}
</style>
